<?php

/**
 * Quincena form.
 *
 * @package    control_obras
 * @subpackage form
 * @author     Your name here
 */
class QuincenaForm extends BaseQuincenaForm
{
  public function setup()
  {
    parent::setup();

    // Widgets
    $this->setWidget('personal_quincena_list', new sfWidgetFormPropelChoice(array('multiple' => true, 'model' => 'Personal')));

    // Validators
    $this->setValidator('personal_quincena_list', new sfValidatorPropelChoice(array('multiple' => true, 'model' => 'Personal', 'required' => false)));    
  }

  public function configure()
  {
    sfContext::getInstance()->getConfiguration()->loadHelpers(array("I18N", 'Tag','Asset'));

    //$this->setWidget('fecha_inicio', new sfWidgetFormJQueryDate(array('date_widget' => new sfWidgetFormDate(array('format' => '%day% / %month% / %year%')), 'image' => image_path('system-calendar.png'))));

    $this->setWidget('fecha_inicio', new sfWidgetFormDate(array('format' => '%day% / %month% / %year%')));

    //$this->setWidget('fecha_fin', new sfWidgetFormJQueryDate(array('date_widget' => new sfWidgetFormDate(array('format' => '%day% / %month% / %year%')), 'image' => image_path('system-calendar.png'))));

    $this->setWidget('fecha_fin', new sfWidgetFormDate(array('format' => '%day% / %month% / %year%')));

    $this->getWidget('personal_quincena_list')->setOption('renderer_class', 'sfWidgetFormSelectDoubleList');
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['personal_quincena_list']))
    {
      $values = array();
      foreach ($this->object->getPersonalQuincenas() as $obj)
      {
        $values[] = $obj->getPersonalId();
      }

      $this->setDefault('personal_quincena_list', $values);
    }
  }

  protected function doSave($con = null)
  {
    parent::doSave($con);

    $this->savePersonalQuincenaList($con);
    $this->saveDiaTrabajadoList($con);
  }

  public function savePersonalQuincenaList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['personal_quincena_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    /*$c = new Criteria();
    $c->add(PersonalQuincenaPeer::QUINCENA_ID, $this->object->getPrimaryKey());
    PersonalQuincenaPeer::doDelete($c, $con);*/

    $values = $this->getValue('personal_quincena_list');
    if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new PersonalQuincena();
        $obj->setQuincenaId($this->object->getPrimaryKey());
        $obj->setPersonalId($value);
        $obj->save();
      }
    }
  }

  public function saveDiaTrabajadoList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['personal_quincena_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $fecha_inicio = new DateTime($this->getValue('fecha_inicio'));
    $fecha_fin = new DateTime($this->getValue('fecha_fin'));
    $fecha_fin = $fecha_fin->modify( '+1 day' );
    $intervalo = new DateInterval('P1D');

    foreach ($this->object->getPersonalQuincenas() as $pq)
    {
      $periodo = new DatePeriod($fecha_inicio, $intervalo, $fecha_fin);

      foreach ($periodo as $fecha)
      {
        $dia_trabajado = new DiaTrabajado();
        $dia_trabajado->setFecha($fecha->format("Y-m-d"));
        $dia_trabajado->setPersonalQuincenaId($pq->getId());
//        $dia_trabajado->setQuincenaId($this->object->getPrimaryKey());
        $dia_trabajado->save();
      }
    }
  }
}
